//
//  Method.swift
//  hc_bio_sdk
//
//  Created by HCMacMini on 2025/4/24.
//

import UIKit
import Flutter
import HCSDK

enum MethodHandler: String {
    
    case initialize
    
    case login
    
    case append
    
}


extension MethodHandler {
    
    func handle(_ call: FlutterMethodCall,
                result: @escaping FlutterResult
    ) {
        switch self {
            /** 初始化*/
        case .initialize:
            guard let params = call.arguments as? [String: Any],
                  let channelCode = params["channelCode"] as? String
            else {
                result(FlutterError(code: "400",
                                    message: "pararms err on \(self)",
                                    details: nil))
                return
            }
            HCSDK.instance.initialize(channelCode: channelCode,
                                      host: "http://47.100.120.192")
            result(nil)
            /** 登录*/
        case .login:
            guard let userID = call.arguments as? String else {
                result(FlutterError(code: "400",
                                    message: "pararms err on \(self)",
                                    details: nil))
                return
            }
            Task {
                do {
                    try await HCSDK.instance.login(userID, mode: .Customized)
                    DispatchQueue.main.async {
                        result(nil)
                    }
                } catch {
                    DispatchQueue.main.async {
                        result(FlutterError(code: "500",
                                            message: " \(error) \(self)",
                                            details: nil))
                    }
                }
            }
        case .append:
            guard let data = call.arguments as? [String : Int] else {
                result(FlutterError(code: "400",
                                    message: "pararms err on \(self)",
                                    details: nil))
                return
            }
            let timestamp = data["timestamp"] ?? 0
            let rr = data["rr"] ?? 0
            ServicePod.bio.append((Double(timestamp),rr,0,0))
            DispatchQueue.main.async {
                result(nil)
            }
        }
        
    }
}
